<?php

namespace app\stock\controller\Manage;

use think\Controller;
use think\Db;

class Proorder extends Controller
{
    // 模型
    protected $OrderModel = null;
    protected $OrderProductModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->OrderModel = model("Order.Order");
        $this->OrderProductModel = model('Order.Product');
    }

    public function index()
    {
        $proOrderListData = $this->OrderModel->order('createtime DESC')->select();

        if (!$proOrderListData) {
            $this->error('暂无数据');
        }

        foreach ($proOrderListData as &$item) {
            $OrderProductData = $this->OrderProductModel->with(['products'])->where(['orderid' => $item['id']])->select();
            // halt($this->OrderProductModel->getLastSql());
            $item['OrderProduct'] = $OrderProductData;
        }

        $this->success('查询成功', null, $proOrderListData);
    }

    public function info()
    {
        $proId = $this->request->param('proId', 0, 'trim');

        $product = $this->OrderModel->find($proId);

        if (!$product) {
            $this->error('商品不存在');
        }

        $this->success('返回数据成功', null, $product);
    }

    public function del()
    {
        $subOrderId = $this->request->param('subOrderId', 0, 'trim');

        $subOrder = $this->OrderModel->find($subOrderId);

        if (!$subOrder) {
            $this->error('课程订单不存在');
        }

        $result = $this->OrderModel->destroy($subOrderId);

        if ($result === FALSE) {
            $this->error('删除失败');
        } else {
            $this->success('删除成功');
        }
    }
}
